/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.ds;

import java.util.LinkedList;
import java.util.List;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.Util;
import net.bioteam.appweb.gwt.apps.clientandserver.ds.DSProxy;
import net.bioteam.appweb.gwt.apps.clientandserver.ds.DSProxyAsync;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

public class DatasetPanel extends SectionStack
{

	public TabSet ts;
	DatasetGrid grid;
	LocalDatasetGrid localGrid;
	ImgButton subscribe;

	public DatasetPanel()
	{
		super();
		setWidth(DatasetGrid.WIDTH + 20);
		setHeight100();

		SectionStackSection section = new SectionStackSection();
		section.setCanCollapse(false);
		section.setExpanded(true);

		ImgButton refresh = new ImgButton();
		refresh.setSrc("[SKIN]actions/refresh.png");
		refresh.setSize(16);
		refresh.setShowFocused(false);
		refresh.setShowRollOver(false);
		refresh.setShowDown(false);
		refresh.setPrompt("reload");
		refresh.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				if (ts.getSelectedTabNumber() == 0)
				{
					grid.reload();
				} else
				{
					localGrid.reload();
				}
			}
		});

		subscribe = new ImgButton();
		subscribe.setSrc("[SKIN]actions/save.png");
		subscribe.setSize(16);
		subscribe.setShowFocused(false);
		subscribe.setShowRollOver(false);
		subscribe.setShowDown(false);
		subscribe.setPrompt("Click to subscribe selected Dataset");
		subscribe.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				if (ts.getSelectedTabNumber() == 0)
				{
					ListGridRecord rds[] = grid.getSelection();
					final List<String> subscribednames = new LinkedList<String>();
					for (ListGridRecord rd : rds)
					{

						subscribednames.add(rd.getAttribute("name"));

					}
					ApplicationCallBack<String> callback = new ApplicationCallBack<String>()
					{

						public void onFailureAfter(Throwable caught)
						{
							SC.warn(caught.getMessage(), StaticHolder.dumpB);
							grid.reload();
						}

						public void onSuccessAfter(String ret)
						{
							// grid.reload();
							StaticHolder.setStatusMessage("subscription is updated", 3);
							localGrid.reload();
							ts.selectTab(1);
						}

						public void execute()
						{

							DSProxyAsync proxy = GWT.create(DSProxy.class);
							proxy.Subscribe(subscribednames, this);
						}

					};
					callback.execute();
				} else
				{
					ListGridRecord rds[] = localGrid.getSelection();
					final List<String> enablednames = new LinkedList<String>();
					for (ListGridRecord rd : rds)
					{

						enablednames.add(rd.getAttribute("name"));

					}
					ApplicationCallBack<String> callback = new ApplicationCallBack<String>()
					{

						public void onFailureAfter(Throwable caught)
						{
							SC.warn(caught.getMessage(), StaticHolder.dumpB);
							grid.reload();
						}

						public void onSuccessAfter(String ret)
						{
							// grid.reload();
							StaticHolder.setStatusMessage("enable/disable status is updated", 3);
						}

						public void execute()
						{

							DSProxyAsync proxy = GWT.create(DSProxy.class);
							proxy.enableDisable(enablednames, this);
						}

					};
					callback.execute();
				}
			}
		});

		ImgButton logBtn = new ImgButton();
		logBtn.setSrc("[SKIN]actions/groupby.png");
		logBtn.setSize(16);
		logBtn.setShowFocused(false);
		logBtn.setShowRollOver(false);
		logBtn.setShowDown(false);
		logBtn.setPrompt("Download Log");
		logBtn.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				Util.open("apps/user/dslog");

			}

		});

		ts = new TabSet();
		ts.setWidth(DatasetGrid.WIDTH + 20);
		ts.setHeight100();

		Tab subTab = new Tab("Subscribe");
		grid = new DatasetGrid();
		grid.load();

		subTab.setPane(grid);
		ts.addTab(subTab);

		Tab enableTab = new Tab("Enable/Disable");
		localGrid = new LocalDatasetGrid();
		localGrid.load();

		enableTab.setPane(localGrid);
		ts.addTab(enableTab);

		ts.addTabSelectedHandler(new TabSelectedHandler()
		{

			public void onTabSelected(TabSelectedEvent event)
			{
				if (event.getTabNum() == 0)
				{
					showSubscribe();
				} else
				{
					showLocal();
				}

			}
		});

		section.setControls(refresh, subscribe, logBtn);
		section.setItems(ts);
		this.setSections(section);

	}

	public void showSubscribe()
	{
		subscribe.setPrompt("subscribe");
		//subscribe.setWidth(60);
	}

	public void showLocal()
	{
		subscribe.setPrompt("enable/disable");
		//subscribe.setWidth(100);
	}

}
